<template>
  <div>
    <div class="flex justify-between items-center mt-2">
      <!-- @change="queryList" -->
      <a-radio-group v-model:value="isDisplayed" button-style="solid">
        <a-radio-button :value="0">有效</a-radio-button>
        <a-radio-button :value="1">全部</a-radio-button>
      </a-radio-group>
      <div>
        <a-button type="primary" class="mr-2" ghost @click="saveSign()">签署</a-button>
        <a-button type="primary" class="mr-2" @click="onClickAdd()">新增申请单</a-button>
      </div>
    </div>
    <template v-if="applyList.length">
      <div style="height: calc(100vh - 260px)" class="mt-2 px-1.5 overflow-auto">
        <div v-for="item in applyList" :key="item.lsh" class="mt-5 mb-2 border border-solid">
          <div class="h-12 border-b border-solid flex items-center bg-thin">
            <div class="name truncate">
              <LabelValue class="ml-10" :width="230" tooltip :value="item?.categoryName" />
            </div>
            <div class="w-[160px]">
              <a-tag v-if="item.status === '未审核'" color="blue">未审核</a-tag>
              <a-tag v-if="item.status === '已审核'" color="green">已审核</a-tag>
              <a-tag v-if="item.status === '已打印'" color="green">已打印</a-tag>
              <a-tag v-if="item.status === '已作废'" color="red">已作废</a-tag>
              <a-tag v-if="item.status === '已签署'" color="red">已签署</a-tag>
            </div>
            <LabelValue class="ml-12" label="科室" tooltip :width="245" :value="item?.applyDeptName" />
            <LabelValue class="ml-1" label="医生" tooltip :width="150" :value="item?.applyDoctorName" />
            <LabelValue class="ml-12" label="申请时间" tooltip :width="227" :value="item?.applyTime" />
          </div>
          <div class="h-16 flex items-center justify-between">
            <div class="flex">
              <LabelValue class="ml-12 mr-8" tooltip :width="360" :value="item.orderItemNames" />
              <LabelValue class="ml-12" label="审核人" tooltip :width="205" :value="item?.auditEmpName" />
              <LabelValue class="ml-12" label="审核时间" tooltip :width="250" :value="item?.auditTime" />
            </div>

            <div class="pr-4">
              <a-button type="link" size="small" :disabled="item.status !== '未审核' || item.status === '已签署'" @click="onClickAdd(item)"> 修改 </a-button>
              <BaseButton
                confirm-text="确认作废?"
                type="text"
                danger
                size="small"
                :disabled="item.status !== '未审核' || item.status === '已签署'"
                @click="onDelete(item)"
              >
                作废
              </BaseButton>

              <a-button type="link" size="small" @click="onPrintReport(item)"> 查阅报告 </a-button>
              <!-- <a-button type="link" size="small"> 查阅影像 </a-button> -->
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-else>
      <a-empty />
    </template>
  </div>
  <Edit v-model:open="open" :row-data="tableRow" @success="queryList()" />
</template>
<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import LabelValue from '@/components/LabelValue'
import userStore from '@/store/user'
import useStore from '../../store'
import Edit from './Edit'

defineProps({
  patientData: {
    type: Object,
    default: () => ({})
  }
})

const { empId } = userStore()

const store = useStore()
const open = ref(false)
const applyList = ref([])
const tableRow = ref({})
const isDisplayed = ref(0)
const tableListId = ref([])
const onClickAdd = (row = {}) => {
  tableRow.value = { ...row, ...store.currentPatient, key: store.currentPatient.jzbh, applyDoctorId: empId, brId: store.currentPatient.brId }
  open.value = true
}

const queryList = async () => {
  const params = {}
  if (isDisplayed.value) {
    params.brId = store.currentPatient.brId
  } else {
    params.key = store.currentPatient.jzbh
  }
  const { code, data, msg } = await post('/ris/apply/getApplyList', { ...params, applyPath: 0 })
  if (code !== 0) return message.error(msg)
  applyList.value = data
  tableListId.value = applyList.value.map((item) => item.orderItemIDs).flat()
}
// 签署
const saveSign = async () => {
  if (!tableListId.value.length) {
    return message.error('不能为空')
  }
  const { code, msg } = await post('/outp/mzys/signedDoctor', {
    yzid: tableListId.value,
    yzzt: 1
  })
  if (code !== 0) return message.error(msg)
  message.destroy()
  message.success('签署成功')
  queryList()
}

// 作废
const onDelete = async (row) => {
  const { code, msg } = await post('/ris/apply/doChangeApplyStatus', { data: row.appSn })
  code ? message.error(msg) : message.success(msg)
  queryList()
}
const onPrintReport = async (item) => {
  const { code, msg, data } = await post('/ris/apply/printTheRisJgbg', { jgbgLsh: item.appSn })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}

// 打印
// const onPrint = async (item, dylx) => {
//   const { code, msg, data } = await post('/lis/lisPrint/printLisRecord', { lsh: item.lsh, dylx })
//   if (code !== 0) return message.error(msg)
//   window.openExe?.rpt(data) //调用winform打印
// }

watchEffect(() => {
  if (store.currentPatient.jzbh) {
    queryList()
  }
})
</script>
<style lang="less" scoped>
.name {
  padding: 0 5px;
  font-weight: 700;
  font-size: 20px;
}
</style>
